package com.example.night.controller;

import com.example.night.data.Influencer.Influencer;
import com.example.night.data.Influencer.InfluencerPackage;
import com.example.night.data.InfluencerOperationRecord.InfluencerOperationRecord;
import com.example.night.service.ApiService.ApiService;
import com.example.night.service.ApiService.InfluencerService;
import com.example.night.service.ApiService.InfluencerSummaryService;
import com.example.night.service.TimeService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/influencers")
public class InfluencerController {

    private final InfluencerSummaryService influencerSummaryService;
    private final TimeService timeService;
    private final InfluencerService influencerService;

    public InfluencerController(InfluencerSummaryService influencerSummaryService, TimeService timeService, InfluencerService influencerService) {
        this.influencerSummaryService = influencerSummaryService;
        this.timeService = timeService;
        this.influencerService = influencerService;
    }

    // 批量插入或更新达人信息
    @PostMapping("/save_or_update_influencers")
    public Map<String, Object> saveOrUpdateInfluencers(@RequestBody List<InfluencerPackage> influencerPackages) {
        if (influencerPackages == null || influencerPackages.isEmpty()) {
            return Map.of(
                    "message", "请求数据为空",
                    "success", false
            );
        }
        influencerService.saveOrUpdateInfluencers(influencerPackages);

        return Map.of(
                "message", "批量达人信息更新成功",
                "success", true
        );
    }

    // 通过 promoteId 更新达人信息
    @PostMapping("/update_by_promote_id")
    public Map<String, Object> updateInfluencerByPromoteId(@RequestParam String promoteId) {
        if (promoteId == null || promoteId.isEmpty()) {
            return Map.of(
                    "message", "promoteId 不能为空",
                    "success", false
            );
        }

        influencerService.updateInfluencerByPromoteId(promoteId);

        return Map.of(
                "message", "达人信息更新成功",
                "success", true
        );

    }

    // **新增: 添加达人到待注册列表**
    @PostMapping("/add_pending_influencers")
    public Map<String, Object> addPendingInfluencers(@RequestBody List<InfluencerPackage> influencerPackages) {
        if (influencerPackages == null || influencerPackages.isEmpty()) {
            return Map.of(
                    "message", "达人列表不能为空",
                    "success", false
            );
        }

        influencerService.addPendingInfluencers(influencerPackages);
        return Map.of(
                "message", "达人成功添加到待注册列表",
                "success", true
        );
    }

    // **新增: 获取所有待注册的达人**
    @GetMapping("/get_pending_influencers")
    public Map<String, Object> getPendingInfluencers() {
        List<InfluencerPackage> pendingInfluencerPackages = influencerService.getPendingInfluencers();

        return Map.of(
                "message", "获取待注册达人列表成功",
                "success", true,
                "data", pendingInfluencerPackages
        );
    }

    // **新增: 通过注册请求**
    @PostMapping("/register_by_promote_id")
    public Map<String, Object> registerInfluencerByPromoteId(@RequestParam String promoteId) {
        if (promoteId == null || promoteId.isEmpty()) {
            return Map.of(
                    "message", "promoteId 不能为空",
                    "success", false
            );
        }

        influencerService.movePendingInfluencerToRegistered(promoteId);

        return Map.of(
                "message", "达人注册成功",
                "success", true
        );
    }

    // **新增: 通过注册请求**
    @PostMapping("/unregister_by_promote_id")
    public Map<String, Object> unRegisterInfluencerByPromoteId(@RequestParam String promoteId) {
        if (promoteId == null || promoteId.isEmpty()) {
            return Map.of(
                    "message", "promoteId 不能为空",
                    "success", false
            );
        }

        influencerService.rejectPendingInfluencer(promoteId);

        return Map.of(
                "message", "注册请求拒绝成功",
                "success", true
        );
    }
    @GetMapping("/get_operation_records")
    public Map<String, Object> getOperationRecords() {
        List<InfluencerOperationRecord> records = influencerService.getPendingInfluencerOperationRecords();
        return Map.of(
                "success", true,
                "data", records
        );
    }

    @GetMapping("/get_influencers_Ranking")
    public Map<String, Object> getInfluencersRanking(
            @RequestParam String start_time,
            @RequestParam String end_time,
            @RequestParam int type,
            @RequestParam String promoterId,
            @RequestParam int page
    ) {
        List<InfluencerPackage> influencersRanking;
        influencersRanking = influencerSummaryService.fetchInfluencers(start_time, end_time, page, type, promoterId);
        return Map.of(
                "total_ranking", influencersRanking.size(),
                "ranking", influencersRanking,
                "success", true
        );
    }


    // 验证登录
    @PostMapping("/login")
    public Map<String, Object> validateLogin(@RequestBody Map<String, String> loginRequest) {
        String phone = loginRequest.get("phone");
        String password = loginRequest.get("password");

        if (phone == null || password == null) {
            return Map.of(
                    "message", "手机号或密码不能为空",
                    "success", false
            );
        }

        boolean isValid = influencerService.validateLogin(phone, password);

        if (isValid) {
            return Map.of(
                    "message", "登录成功",
                    "success", true
            );
        } else {
            return Map.of(
                    "message", "手机号或密码错误",
                    "success", false
            );
        }
    }
    @GetMapping("/find_by_phone")
    public Map<String, Object> findInfluencersByPhone(@RequestParam String phone) {
        List<InfluencerPackage> influencerPackages = influencerService.findInfluencersByPhone(phone);

        if (!influencerPackages.isEmpty()) {
            return Map.of(
                    "message", "查询成功",
                    "success", true,
                    "data", influencerPackages
            );
        } else {
            return Map.of(
                    "message", "未找到该手机号的达人信息",
                    "success", false,
                    "data", List.of() // 返回空数组，而不是 null，避免前端异常
            );
        }
    }

}
